﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntraVision.Web;
using IntraVision.Web.Security;
using Model;

public class CompanyPersonController : EntityListController<CompanyPerson, NotarialServiceDataContext>
{
    public void AddCompanyPerson()
    {
        int personId = 0; int companyId = 0;
        if (!string.IsNullOrEmpty(Request["personid"]) && int.TryParse(Request["personid"], out personId) && personId > 0
            && !string.IsNullOrEmpty(Request["companyid"]) && int.TryParse(Request["companyid"], out companyId) && companyId > 0)
        {
            using (var dc = new NotarialServiceDataContext())
            {
                dc.CompanyPersons.InsertOnSubmit(new CompanyPerson { CompanyId = companyId, PersonId = personId });
                dc.SubmitChanges();
            }
        }
    }

    public void DeleteCompanyPerson()
    {
        int[] ids = RequestHelper.GetIdsFromString(Request["ids"]);
        int personId = 0;
        if (ids.Length > 0 && int.TryParse(Request["personid"], out personId))
        {
            using (var dc = new NotarialServiceDataContext())
            {
                dc.CompanyPersons.DeleteAllOnSubmit((from sc in dc.CompanyPersons where sc.PersonId == personId 
                                                     && ids.Contains(sc.CompanyId) select sc));
                if (dc.GetChangeSet().Deletes.Count() > 0)
                    dc.SubmitChanges();
            }
        }
    }

    public void DeletePersonCompany()
    {                
        int[] ids = RequestHelper.GetIdsFromString(Request["ids"]);
        int companyId = 0;
        if (ids.Length > 0 && int.TryParse(Request["companyid"], out companyId))
        {
            using (var dc = new NotarialServiceDataContext())
            {
                dc.CompanyPersons.DeleteAllOnSubmit((from sc in dc.CompanyPersons where sc.CompanyId == companyId 
                                                       && ids.Contains(sc.PersonId) select sc));
                if (dc.GetChangeSet().Deletes.Count() > 0)
                    dc.SubmitChanges();
            }
        }
    }
}
